home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / nslookup / RCS / debug.c,v < prev    next >
Encoding:
Text File  |  1988-11-27  |  9.1 KB  |  426 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     88.11.23.13.38.53;  author douglis;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @original src from monet.
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/*
  27.  * Copyright (c) 1985 Regents of the University of California.
  28.  * All rights reserved.
  29.  *
  30.  * Redistribution and use in source and binary forms are permitted
  31.  * provided that the above copyright notice and this paragraph are
  32.  * duplicated in all such forms and that any documentation,
  33.  * advertising materials, and other materials related to such
  34.  * distribution and use acknowledge that the software was developed
  35.  * by the University of California, Berkeley.  The name of the
  36.  * University may not be used to endorse or promote products derived
  37.  * from this software without specific prior written permission.
  38.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  39.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  40.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  41.  */
  42.  
  43. #ifndef lint
  44. static char sccsid[] = "@@(#)debug.c    5.13 (Berkeley) 6/18/88";
  45. #endif /* not lint */
  46.  
  47. /*
  48.  *******************************************************************************
  49.  *
  50.  *  debug.c --
  51.  *
  52.  *    Routines to print out packets received from a name server query.
  53.  *
  54.  *      Modified version of 4.3BSD BIND res_debug.c 5.6 9/14/85
  55.  *
  56.  *******************************************************************************
  57.  */
  58.  
  59. #include <sys/types.h>
  60. #include <netinet/in.h>
  61. #include <stdio.h>
  62. #include <arpa/nameser.h>
  63. #include <resolv.h>
  64. #include <netdb.h>
  65. #include "res.h"
  66.  
  67. extern char ctime();
  68.  
  69. /*
  70.  *  Imported from res_debug.c
  71.  */
  72. extern char *_res_resultcodes[];
  73. extern char *_res_opcodes[];
  74.  
  75. /*
  76.  *  Used to highlight the start of a record when printing it.
  77.  */
  78. #define INDENT "    ->  "
  79.  
  80.  
  81.  
  82. /*
  83.  * Print the contents of a query.
  84.  * This is intended to be primarily a debugging routine.
  85.  */
  86.  
  87. Print_query(msg, eom, printHeader)
  88.     char *msg, *eom;
  89.     int printHeader;
  90. {
  91.     Fprint_query(msg, eom, printHeader,stdout);
  92. }
  93.  
  94. Fprint_query(msg, eom, printHeader,file)
  95.     char *msg, *eom;
  96.     int printHeader;
  97.     FILE *file;
  98. {
  99.     register char *cp;
  100.     register HEADER *hp;
  101.     register int n;
  102.     short class;
  103.     short type;
  104.  
  105.     /*
  106.      * Print header fields.
  107.      */
  108.     hp = (HEADER *)msg;
  109.     cp = msg + sizeof(HEADER);
  110.     if (printHeader || (_res.options & RES_DEBUG2)) {
  111.         fprintf(file,"    HEADER:\n");
  112.         fprintf(file,"\topcode = %s", _res_opcodes[hp->opcode]);
  113.         fprintf(file,", id = %d", ntohs(hp->id));
  114.         fprintf(file,", rcode = %s\n", _res_resultcodes[hp->rcode]);
  115.         fprintf(file,"\theader flags: ");
  116.         if (hp->qr) {
  117.             fprintf(file," response");
  118.         } else {
  119.             fprintf(file," query");
  120.         }
  121.         if (hp->aa)
  122.             fprintf(file,", auth. answer");
  123.         if (hp->tc)
  124.             fprintf(file,", truncation");
  125.         if (hp->rd)
  126.             fprintf(file,", want recursion");
  127.         if (hp->ra)
  128.             fprintf(file,", recursion avail.");
  129.         if (hp->pr)
  130.             fprintf(file,", primary");
  131.         fprintf(file,"\n\tquestions = %d", ntohs(hp->qdcount));
  132.         fprintf(file,",  answers = %d", ntohs(hp->ancount));
  133.         fprintf(file,",  auth. records = %d", ntohs(hp->nscount));
  134.         fprintf(file,",  additional = %d\n\n", ntohs(hp->arcount));
  135.     }
  136.  
  137.     /*
  138.      * Print question records.
  139.      */
  140.     if (n = ntohs(hp->qdcount)) {
  141.         fprintf(file,"    QUESTIONS:\n");
  142.         while (--n >= 0) {
  143.             fprintf(file,"\t");
  144.             cp = Print_cdname(cp, msg, eom, file);
  145.             if (cp == NULL)
  146.                 return;
  147.             type = _getshort(cp);
  148.             cp += sizeof(u_short);
  149.             class = _getshort(cp);
  150.             cp += sizeof(u_short);
  151.             fprintf(file,", type = %s", p_type(type));
  152.             fprintf(file,", class = %s\n", p_class(class));
  153.         }
  154.     }
  155.     /*
  156.      * Print authoritative answer records
  157.      */
  158.     if (n = ntohs(hp->ancount)) {
  159.         fprintf(file,"    ANSWERS:\n");
  160.         while (--n >= 0) {
  161.             fprintf(file, INDENT);
  162.             cp = Print_rr(cp, msg, eom, file);
  163.             if (cp == NULL)
  164.                 return;
  165.         }
  166.     }
  167.     /*
  168.      * print name server records
  169.      */
  170.     if (n = ntohs(hp->nscount)) {
  171.         fprintf(file,"    NAME SERVERS:\n");
  172.         while (--n >= 0) {
  173.             fprintf(file, INDENT);
  174.             cp = Print_rr(cp, msg, eom, file);
  175.             if (cp == NULL)
  176.                 return;
  177.         }
  178.     }
  179.     /*
  180.      * print additional records
  181.      */
  182.     if (n = ntohs(hp->arcount)) {
  183.         fprintf(file,"    ADDITIONAL RECORDS:\n");
  184.         while (--n >= 0) {
  185.             fprintf(file, INDENT);
  186.             cp = Print_rr(cp, msg, eom, file);
  187.             if (cp == NULL)
  188.                 return;
  189.         }
  190.     }
  191.     fprintf(file,"\n------------\n");
  192.  
  193. }
  194.  
  195.  
  196. char *
  197. Print_cdname_sub(cp, msg, eom, file, format)
  198.     char *cp, *msg, *eom;
  199.     FILE *file;
  200.     int format;
  201. {
  202.     int n;
  203.     char name[MAXDNAME];
  204.     extern char *strcpy();
  205.  
  206.     if ((n = dn_expand(msg, eom, cp, name, sizeof(name))) < 0)
  207.         return (NULL);
  208.     if (name[0] == '\0') {
  209.         (void) strcpy(name, "(root)");
  210.     }
  211.     if (format) {
  212.         fprintf(file, "%-30s", name);
  213.     } else {
  214.         fputs(name, file);
  215.     }
  216.     return (cp + n);
  217. }
  218.  
  219. char *
  220. Print_cdname(cp, msg, eom, file)
  221.     char *cp, *msg, *eom;
  222.     FILE *file;
  223. {
  224.     return(Print_cdname_sub(cp, msg, eom, file, 0));
  225. }
  226.  
  227. char *
  228. Print_cdname2(cp, msg, eom, file)
  229.     char *cp, *msg, *eom;
  230.     FILE *file;
  231. {
  232.     return(Print_cdname_sub(cp, msg, eom, file, 1));
  233. }
  234.  
  235. /*
  236.  * Print resource record fields in human readable form.
  237.  */
  238. char *
  239. Print_rr(cp, msg, eom, file)
  240.     char *cp, *msg, *eom;
  241.     FILE *file;
  242. {
  243.     int type, class, dlen, n, c;
  244.     long ttl;
  245.     struct in_addr inaddr;
  246.     char *cp1;
  247.  
  248.     if ((cp = Print_cdname(cp, msg, eom, file)) == NULL) {
  249.         fprintf(file, "(name truncated?)\n");
  250.         return (NULL);            /* compression error */
  251.     }
  252.  
  253.     type = _getshort(cp);
  254.     cp += sizeof(u_short);
  255.     class = _getshort(cp);
  256.     cp += sizeof(u_short);
  257.     ttl = _getlong(cp);
  258.     cp += sizeof(u_long);
  259.     dlen = _getshort(cp);
  260.     cp += sizeof(u_short);
  261.  
  262.     if (_res.options & RES_DEBUG2) {
  263.         fprintf(file,"\n\ttype = %s, class = %s, ttl = %s, dlen = %d",
  264.             p_type(type), p_class(class), p_time(ttl), dlen);
  265.         fprintf(file,"\n");
  266.     }
  267.  
  268.     cp1 = cp;
  269.  
  270.     /*
  271.      * Print type specific data, if appropriate
  272.      */
  273.     switch (type) {
  274.     case T_A:
  275.         switch (class) {
  276.         case C_IN:
  277.             bcopy(cp, (char *)&inaddr, sizeof(inaddr));
  278.             if (dlen == 4) {
  279.                 fprintf(file,"\tinet address = %s\n",
  280.                     inet_ntoa(inaddr));
  281.                 cp += dlen;
  282.             } else if (dlen == 7) {
  283.                 fprintf(file,"\tinet address = %s",
  284.                     inet_ntoa(inaddr));
  285.                 fprintf(file,", protocol = %d", cp[4]);
  286.                 fprintf(file,", port = %d\n",
  287.                     (cp[5] << 8) + cp[6]);
  288.                 cp += dlen;
  289.             }
  290.             break;
  291.         default:
  292.             fprintf(file,"\taddress, class = %d, len = %d\n",
  293.                 class, dlen);
  294.         }
  295.         break;
  296.  
  297.     case T_CNAME:
  298.         fprintf(file,"\tcanonical name = ");
  299.         goto doname;
  300.  
  301.     case T_MX:
  302.         fprintf(file,"\tpreference = %d",_getshort(cp));
  303.         cp += sizeof(u_short);
  304.         fprintf(file,", mail exchanger = ");
  305.         goto doname;
  306.  
  307.     case T_MG:
  308.         fprintf(file,"\tmail group member = ");
  309.         goto doname;
  310.     case T_MB:
  311.         fprintf(file,"\tmail box = ");
  312.         goto doname;
  313.     case T_MR:
  314.         fprintf(file,"\tmailbox rename = ");
  315.         goto doname;
  316.     case T_NS:
  317.         fprintf(file,"\tnameserver = ");
  318.         goto doname;
  319.     case T_PTR:
  320.         fprintf(file,"\thost name = ");
  321. doname:
  322.         cp = Print_cdname(cp, msg, eom, file);
  323.         fprintf(file,"\n");
  324.         break;
  325.  
  326.     case T_HINFO:
  327.         if (n = *cp++) {
  328.             fprintf(file,"\tCPU=%.*s", n, cp);
  329.             cp += n;
  330.         }
  331.         if (n = *cp++) {
  332.             fprintf(file,"\tOS=%.*s\n", n, cp);
  333.             cp += n;
  334.         }
  335.         break;
  336.  
  337.     case T_SOA:
  338.         fprintf(file,"\torigin = ");
  339.         cp = Print_cdname(cp, msg, eom, file);
  340.         fprintf(file,"\n\tmail addr = ");
  341.         cp = Print_cdname(cp, msg, eom, file);
  342.         fprintf(file,"\n\tserial=%ld", _getlong(cp));
  343.         cp += sizeof(u_long);
  344.         fprintf(file,", refresh=%s", p_time(_getlong(cp)));
  345.         cp += sizeof(u_long);
  346.         fprintf(file,", retry=%s\n", p_time(_getlong(cp)));
  347.         cp += sizeof(u_long);
  348.         fprintf(file,"\texpire=%s", p_time(_getlong(cp)));
  349.         cp += sizeof(u_long);
  350.         fprintf(file,", min=%s\n", p_time(_getlong(cp)));
  351.         cp += sizeof(u_long);
  352.         break;
  353.  
  354.     case T_MINFO:
  355.         fprintf(file,"\trequests = ");
  356.         cp = Print_cdname(cp, msg, eom, file);
  357.         fprintf(file,"\n\terrors = ");
  358.         cp = Print_cdname(cp, msg, eom, file);
  359.         break;
  360.  
  361.     case T_UINFO:
  362.         fprintf(file,"\t%s\n", cp);
  363.         cp += dlen;
  364.         break;
  365.  
  366.     case T_UID:
  367.     case T_GID:
  368.         if (dlen == 4) {
  369.             fprintf(file,"\t%cid %ld\n", type == T_UID ? 'u' : 'g',
  370.                 _getlong(cp));
  371.             cp += sizeof(int);
  372.         } else
  373.             fprintf(file,"\t%cid of length %ld?\n",
  374.                 type == T_UID ? 'u' : 'g', dlen);
  375.         break;
  376.  
  377.     case T_WKS: {
  378.         struct protoent *protoPtr;
  379.  
  380.         if (dlen < sizeof(u_long) + 1)
  381.             break;
  382.         bcopy(cp, (char *)&inaddr, sizeof(inaddr));
  383.         cp += sizeof(u_long);
  384.         if ((protoPtr = getprotobynumber(*cp)) != NULL) {
  385.             fprintf(file,"\tinet address = %s, protocol = %s\n\t",
  386.             inet_ntoa(inaddr), protoPtr->p_name);
  387.         } else {
  388.             fprintf(file,"\tinet address = %s, protocol = %d\n\t",
  389.             inet_ntoa(inaddr), *cp);
  390.         }
  391.         cp++;
  392.         n = 0;
  393.         while (cp < cp1 + dlen) {
  394.             c = *cp++;
  395.             do {
  396.                 struct servent *s;
  397.  
  398.                  if (c & 0200) {
  399.                     s = getservbyport(n, NULL);
  400.                     if (s != NULL) {
  401.                         fprintf(file,"  %s", s->s_name);
  402.                     } else {
  403.                         fprintf(file," #%d", n);
  404.                     }
  405.                 }
  406.                  c <<= 1;
  407.             } while (++n & 07);
  408.         }
  409.         putc('\n',file);
  410.         }
  411.         break;
  412.  
  413.     case T_NULL:
  414.         fprintf(file, "(type NULL, dlen %d)\n", dlen);
  415.         break;
  416.  
  417.     default:
  418.         fprintf(file,"\t???\n");
  419.         cp += dlen;
  420.     }
  421.     if (cp != cp1 + dlen)
  422.         fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);
  423.     return (cp);
  424. }
  425. @
  426.